package com.ygqh.baby.annotation;

import java.util.Collection;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.redis.RedisDao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Component
@Aspect
public class YgCacheAspect {

	private static final int CACHE_MINUTE = 5;
	@Autowired
	private RedisDao redisDao;

	/**
	 * 定义Pointcut，Pointcut的名称 就是simplePointcut，此方法不能有返回值，该方法只是一个标示
	 */
	@Pointcut("@annotation(com.ygqh.baby.annotation.YgController)")
	public void controllerAspect() {
	}

	@AfterReturning(pointcut = "controllerAspect()", returning = "returnValue")
	public void simpleAdvice(JoinPoint jp, JSONPObject returnValue) {
		Message resultMsg = (Message) returnValue.getValue();
		if (Message.Type.success.equals(resultMsg.getType())) {

			String methodName = jp.getSignature().getName(); // 获得方法名
			Signature signature = jp.getSignature();
			// MethodSignature methodSignature = (MethodSignature) signature;
			// String[] parameterNames = methodSignature.getParameterNames();
			Object[] args = jp.getArgs();

			// 商品详情页促销信息
			if ("findOwnPromotionListNotLogin".equals(methodName)) {
				String cacheKey = methodName + "_" + args[1];
				int expire = CACHE_MINUTE * 60 * 1000;
				this.cacheing(cacheKey, expire, resultMsg);
			}
			// 相似晒单
			if ("similarityPublish".equals(methodName)) {

				String cacheKey = methodName + "_" + args[1] + "_" + args[2];
				int expire = CACHE_MINUTE * 72 * 60 * 1000;// 6小时
				this.cacheing(cacheKey, expire, resultMsg);
			}

			// 搜索返回值
			if ("getSearchKeywords".equals(methodName)) {
				int expire = CACHE_MINUTE * 60 * 1000;// 五分钟
				this.cacheing(methodName, expire, resultMsg);
			}

			// 推荐商品
			if ("getProductRecommend".equals(methodName)) {

				String cacheKey = methodName + "_" + args[0];
				int expire = CACHE_MINUTE * 60 * 1000;
				this.cacheing(cacheKey, expire, resultMsg);
			}

			// new首页接口
			if ("newSearch4Index".equals(methodName)) {
				for (Object arg : args) {
					if (arg instanceof QueryInfo) {
						QueryInfo queryInfo = (QueryInfo) arg;
						Integer start = queryInfo.getStart();
						if (start == 0) {
							this.cacheing(methodName, CACHE_MINUTE * 60 * 1000, resultMsg);
						}
					}
				}

			}
		}
	}

	private void cacheing(String cacheKey, int expire, Message resultMsg) {
		String result = redisDao.get(cacheKey);
		if (StringUtil.isBlank(result)) {
			Object extra = resultMsg.getExtra();
			if (extra instanceof Collection) {

				JSONArray obj = JSONArray.fromObject(resultMsg.getExtra());
				System.out.println(obj.toString());
				redisDao.set(cacheKey, obj.toString(), expire);
			} else {
				JSONObject obj = JSONObject.fromObject(resultMsg.getExtra());
				redisDao.set(cacheKey, obj.toString(), expire);
			}
		}
	}

}
